package com.itheima.job;

public class Test01 {
    public static void main(String[] args) {
        Seller s = new Seller();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int index = 0;
                syn(index, s);
            }
        },"实体店").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int index = 0;
                syn(index, s);
            }
        },"官网").start();
    }

    private static void syn(int index, Seller s) {
        while (true) {
            synchronized (s.getObj()) {
                if (s.getCount() == 0) {
                    System.out.println(Thread.currentThread().getName() +"卖完了,卖了" + index + "个。");
                    break;
                } else {
                    index++;
                    s.setCount(s.getCount() - 1);
                    System.out.println(Thread.currentThread().getName() + "在卖水杯，卖出了第" + index + "个，还剩" + s.getCount() + "个");
                }
            }
        }
    }

}


class Seller{
    private int count;

    public Object getObj() {
        return obj;
    }

    private final Object obj = new Object();

    public Seller() {
        this(1000);
    }

    public Seller(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "Seller{" +
                "count=" + count +
                '}';
    }
}
